import { Button, Space, Tag, Popconfirm,Table, type TableProps } from 'antd';
import UseForm from './UseForm';
import react, { useState } from 'react';
import {
  Cascader,
  DatePicker,
  Form,
  Input,
  InputNumber,
  Radio,
  Select,
  Switch,
  TreeSelect,
  Modal,
  Checkbox,
  Row,
  Col
} from 'antd';
interface DataType {
  key: string;
  name: string;
  role: number;
  desc: string;
  tags: string[];
}
import type { FormProps } from 'antd';
 function column(props:any ) {

  const [isModalOpen, setIsModalOpen] = useState(false);
  const [list,setlist]=useState("")
  const showModal = () => {
    setIsModalOpen(true);
  };

  let handleOk = () => {
    setIsModalOpen(false);
  };

  const handleCancel = () => {
    setIsModalOpen(false);
  };
  const columns: TableProps<DataType>['columns'] = [
    {
      title: '用户名',
      dataIndex: 'name',
      key: 'name',
      render: (text) => <a>{text}</a>,
      fixed: 'left',
      width: 100
    },
    {
      title: '角色',
      dataIndex: 'role',
      key: 'role',
      render: role => role === 1 ? '超级管理员' : '开发者'
    },
    {
      title: '简介',
      dataIndex: 'desc',
      key: 'desc',
    },
    {
      title: '标签',
      key: 'tags',
      dataIndex: 'tags',
      render: (_, { tags }) => (
        <>
          {tags.map((tag) => {
            let color = tag.length > 5 ? 'geekblue' : 'green';
            if (tag === 'loser') {
              color = 'volcano';
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            );
          })}
        </>
      ),
    },
    {
      title: '操作',
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <Button onClick={() => {
            setlist("编辑")
            showModal()
          }}>编辑</Button>
          <Popconfirm
            title="删除弹窗"
            description="你确定要删除吗?"
            onConfirm={() => {
              let newdata = data.filter(item => {
                return item.key !=record.key
              })
              setdata(newdata)
              
            }}
            okText="确定"
            cancelText="取消"
          >
            <Button danger>删除</Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];

  let [data, setdata] = useState<DataType[]>([
    {
      key: '1',
      name: '徐小夕',
      role: 1,
      desc: 'H5-Dooring作者, 掘金签约作者，知乎专栏作家',
      tags: ['前端工程师', 'developer'],
    },
    {
      key: '2',
      name: '张三',
      role: 2,
      desc: '知乎专栏作家',
      tags: ['前端工程师', 'developer', 'Dooring'],
    },
    {
      key: '3',
      name: '李盟',
      role: 2,
      desc: 'Dooring共建者',
      tags: ['后端工程师', 'V6.Dooring', 'Dooring'],
    },
    {
      key: '4',
      name: '王阿明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '5',
      name: '张小明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '6',
      name: 'Tom',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    },
    {
      key: '7',
      name: 'Json',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    },
  ]);
      // 提交
      const onFinish: FormProps['onFinish'] = (values) => {
        if(props.list=="添加"){
          let obj={
            key:new Date().getTime(),
            ...values
          }
          setdata([...data,
            obj
          ])
          props.handleOk()
        }
      };
  return (
    <>

     <Modal title={props.list} footer key={new Date().getTime()} open={props.isModalOpen} onOk={props.handleOk} onCancel={props.handleCancel}>
      <Form
        onFinish={onFinish}
        layout="horizontal"

        style={{ maxWidth: 600 }}
      >
        <Form.Item label="用户名" name='name'>
          <Input />
        </Form.Item>
        <Form.Item label="角色" name='role'>
          <Select>
            <Select.Option value="1">超级管理员</Select.Option>
            <Select.Option value="2">开发者</Select.Option>
          </Select>
        </Form.Item>
        <Form.Item label="简介" name='desc'>
          <Input />
        </Form.Item>
        <Form.Item label="标签" name='tags'>
          <Checkbox.Group style={{ width: '100%' }} >
            <Row>
              <Col span={6}>
                <Checkbox value="前端工程师">前端工程师</Checkbox>
              </Col>
              <Col span={6}>
                <Checkbox value="DEVELOPER">DEVELOPER</Checkbox>
              </Col>
              <Col span={6}>
                <Checkbox value="DOORING">DOORING</Checkbox>
              </Col>
              <Col span={6}>
                <Checkbox value="后端工程师">后端工程师</Checkbox>
              </Col>
              <Col span={6}>
                <Checkbox value="V6.DOORING">V6.DOORING</Checkbox>
              </Col>
              <Col span={6}>
                <Checkbox value="全栈工程师">全栈工程师</Checkbox>
              </Col>
              <Col span={6}>
                <Checkbox value="橙子表单">橙子表单</Checkbox>
              </Col>
            </Row>
          </Checkbox.Group>
        </Form.Item>
        <Button ype="primary" htmlType="submit">确认</Button>
      </Form>



    </Modal>
    <div><Table  columns={columns} dataSource={data} pagination={{ pageSize: 5 }} scroll={{ x: 1000 }} /></div>
    </>
  )
}
export default column